/*
 * Copyright 2010 - Jeremie Brebec
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.jb.coinche.server.business.transition;

import org.jb.coinche.client.business.message.NextTurnMessage;
import org.jb.coinche.client.business.model.Player;
import org.jb.coinche.client.business.model.Round;
import org.jb.coinche.client.business.model.RoundFinished;
import org.jb.coinche.client.business.model.RoundTurn;
import org.jb.coinche.client.business.model.RoundTurnFinished;
import org.jb.coinche.client.business.model.Trump;
import org.jb.statemachine.server.Message;
import org.jb.statemachine.server.Transition;

public class RoundTurnFinishedTransition implements Transition {

	@Message
	public Object processNextTurn( RoundTurnFinished rtf, NextTurnMessage msg ) {
		
		if (rtf.getRound().isFinished()) {
			/*
			 * La partie est finie, on calcul qui a gagné
			 */
			RoundFinished rf = new RoundFinished();
			rf.setGame( rtf.getRound().getGame() );
			Round r = rtf.getRound();
			
			r.addScoreDixDeDer( rtf.getWinner().getTeam() );
			
			Player.Team auctionTeam = r.getAuction().getTeam();
			Player.Team otherTeam = (auctionTeam == Player.Team.T1 ? Player.Team.T2 : Player.Team.T1);
			
			rf.setAuction( rtf.getRound().getAuction() );
			
			int pointGagnantContrat = r.getAuction().getValue();
			if (pointGagnantContrat == 160)
				pointGagnantContrat = 250; // capot
			
			/*
			 * Modulation des points à gagner suivant l'etat de la coinche
			 */
			boolean advpt = false; // L'adversaire peut gagner des points ?
			switch( r.getAuction().getCoincheMode() ) {
			case COINCHE:
				pointGagnantContrat *= 2;
				break;
				
			case SURCOINCHE:
				pointGagnantContrat *= 4;
				break;
				
			default:
				advpt = true;
				break;
			}
			
			/*
			 * Score de la partie
			 * 
			 * Tout at' -> On multiplie par 2/3
			 */
			int scoreAuctionTeam = r.getScore(auctionTeam);
			int scoreOtherTeam = r.getScore(otherTeam);

			if (r.getAuction().getTrump() == Trump.TOUTAT) {
				scoreAuctionTeam = (2 * scoreAuctionTeam )/ 3;
				scoreOtherTeam = (2 * scoreOtherTeam ) / 3;
			}
			
			rf.setTeamScore( auctionTeam, scoreAuctionTeam );
			rf.setTeamScore( otherTeam, scoreOtherTeam );
			
			/*
			 * Vérification du contrat
			 */
			if (scoreAuctionTeam >= r.getAuction().getValue() ) {

				/*
				 * Contrat reussi
				 */
				
				/*
				 * Capot ?
				 */
				if (scoreOtherTeam == 0)
					scoreAuctionTeam = 250;
				
				rf.setTeamScoreInc( auctionTeam, scoreAuctionTeam + pointGagnantContrat );
				
				if (advpt)
					rf.setTeamScoreInc( otherTeam, scoreOtherTeam );
				
				rf.setWinner(auctionTeam);
				
			} else {

				/*
				 * Contrat perdu
				 */
				rf.setTeamScoreInc( otherTeam, 160 + pointGagnantContrat );
				
				rf.setWinner(otherTeam);
			}

			r.getGame().addScore1( rf.getTeam1ScoreInc() );
			r.getGame().addScore2( rf.getTeam2ScoreInc() );
			
			return rf;
			
			
		} else {
			/*
			 * On lance juste un nouveau tour
			 */
			RoundTurn roundTurn = new RoundTurn();
			roundTurn.setCurrentPlayer( rtf.getWinner() );
			roundTurn.setRound( rtf.getRound() );
			
			return roundTurn;
		}
	}
}
